﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SeaBattle.Domain.Entities
{

    public class Fleet
    {
        private readonly byte maxShipCount;
        private readonly byte maxPartCount;

        public int Id { get; set; }

        private Dictionary<Ship, ShipOrientation> ships = new Dictionary<Ship, ShipOrientation>();

        /// <summary>
        /// Initializes a new instance of the <see cref="Fleet"/> class.
        /// </summary>
        /// <param name="maxShipCount">The max ship count.</param>
        /// <param name="maxPartCount">The max part count.</param>
        public Fleet(byte maxShipCount, byte maxPartCount)
        {
            this.maxPartCount = maxPartCount;
            this.maxShipCount = maxShipCount;
        }


        /// <summary>
        /// Determines whether this instance [can add ship] the specified ship.
        /// </summary>
        /// <param name="ship">The ship.</param>
        /// <returns>
        ///   <c>true</c> if this instance [can add ship] the specified ship; otherwise, <c>false</c>.
        /// </returns>
        private bool CanAddShip(Ship ship)
        {
            if (ships.Count + 1 > maxShipCount)
                return false;

            var Count = (from someShip in ships select ship.PartsCount).Sum();


            if (Count + ship.PartsCount > maxPartCount)
                return false;
            return true;
        }

        /// <summary>
        /// Adds the ship.
        /// </summary>
        /// <param name="ship">The ship.</param>
        /// <param name="orientation">The orientation.</param>
        public void AddShip(Ship ship, ShipOrientation orientation)
        {
            if (CanAddShip(ship))
            {
                ships.Add(ship,orientation);
            }
        }

        /// <summary>
        /// Removes the ship.
        /// </summary>
        /// <param name="ship">The ship.</param>
        public void RemoveShip(Ship ship)
        {
            ships.Remove(ship);
        }

        public void ResetFleet()
        {
            ships.Clear();
        
        }





    }

    public enum ShipOrientation
    {
        ORIENTATION_VERTICAL = 0,
        ORIENTATION_HORISONTAL
    }

}
